
#include "base_conf.h"
#include "common.h"
#include "util.h"


bool BaseConfig::Init(const char *filename) {
  FILE *fp;
  char buf[512];
  bool ret = false;

  if ((fp = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "open config file fail:%s\n", strerror(errno));
    return false;
  }

  while (fgets(buf, sizeof(buf), fp)) {
    char *line = Util::Lstrip(buf);
    ParseLine(line);
  }

  fclose(fp);

  GotKeyValMap(kv_map_);
  return true;
}

void BaseConfig::ParseLine(const char *buf) {
  if (buf == NULL || *buf == '#')
    return;
  
  char *val = strchr(buf, '=');
  if (val == NULL)
    return;

  *val++ = '\0';
  Util::Rstrip(val);
  kv_map_[buf] = val;
  GotKeyVal(buf, val); 
}
